<template>
    <el-select v-auto-resize v-model="model" placeholder="请选择" style="width:100%" @change="change">
        <el-option v-for="item in list" :key="item.provinceId" :disabled="disabledOptions&&disabledOptions.indexOf(item.provinceId) > -1" :label="item.provinceName" :value="item.provinceId">
        </el-option>
    </el-select>
</template>

<script>
import { queryAllProvince } from 'api/common';

export default {
    props: {
        value: {
            type: [String, Number]
        },
        disabledOptions: Array,
        emptyOption: {
            type: Boolean,
            default: true
        }
    },
    computed: {
        model: {
            get() {
                if (this.list) {
                    if (this.list.some(item => item.provinceId == this.value)) {
                        let provinceObj = this.list.find((item) => {
                            return item.provinceId === this.value;
                        });
                        this.$emit('getProvinceName', provinceObj.provinceName); 
                        return this.value;
                    } else {
                        this.$emit('input', '');
                        return '';
                    }
                } else {
                    return '';
                }
            },
            set(val) {
                this.$emit('input', val);
            }
        }
    },
    data() {
        return {
            list: null
        }
    },
    created() {
        this.getList();
        
    },
    methods: {
        getList() {
            queryAllProvince().then(res => {
                this.list = res.data.content;
                if (this.emptyOption) {
                    this.list.unshift({
                        provinceId: '',
                        provinceName: '请选择'
                    });
                }
            }).catch(res=>{
                setTimeout(()=>{
                    this.getList();
                },Math.random().toFixed(3)*1500)
            });
        },
        change(val) {
            this.$emit('change', val);
        }
    }
}
</script>

<style>

</style>
